package com.lancoo.answer.view.fragment.correctmistake

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.lancoo.answer.databinding.EvFmtGctReviewBinding
import com.lancoo.answer.model.AnswerConstant
import com.lancoo.answer.model.ConstantBean
import com.lancoo.answer.model.TaskState
import com.lancoo.answer.model.bean.ModelAnswerArea
import com.lancoo.answer.model.bean.Ques
import com.lancoo.answer.model.bean.TextInfo
import com.lancoo.answer.util.ExamFontSizeAdjustUtils
import com.lancoo.answer.util.HtmlUtils
import com.lancoo.answer.view.adapter.CorrectMistakesTextAdapter
import com.lancoo.answer.view.base.BaseBindingFragment
import com.lancoo.answer.widget.DrawView

/**
 * Created by wealive on 2022/3/22.
 *
 *
 */
class LookingCorrectMistakesFragment : BaseBindingFragment<EvFmtGctReviewBinding>() {
    private val textInfoList = mutableListOf<TextInfo>()
    private lateinit var adapter: CorrectMistakesTextAdapter
    private var GenreType = 0
    private var GenreID = 0
    private val answerArea = mutableListOf<ModelAnswerArea>()

    private lateinit var ques: Ques
    private var typeIndex = 0
    private var quesIndex = 0
    private var defaultLoadIndex = 0


    companion object {
        fun loadFragment(typeIndex: Int, quesIndex: Int, defaultLoadIndex: Int): Fragment {
            val mBundle = Bundle()
            mBundle.putInt(AnswerConstant.KEY_TYPEINDEX, typeIndex)
            mBundle.putInt(AnswerConstant.KEY_QUESINDEX, quesIndex)
            mBundle.putInt("loadQuesIndex", defaultLoadIndex)
            val lookingCorrectMistakesFragment = LookingCorrectMistakesFragment()
            lookingCorrectMistakesFragment.arguments = mBundle
            return lookingCorrectMistakesFragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initAnswerData()
    }

    @SuppressLint("SetTextI18n")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (!ques.QuesIndexText.isNullOrEmpty()) {
            binding.tvLeader.text = HtmlUtils.setText(ques.QuesIndexText!!)
            binding.tvLeader.visibility = View.VISIBLE
        } else {
            binding.tvLeader.visibility = View.INVISIBLE
        }
        adapter = CorrectMistakesTextAdapter(1, requireContext(), textInfoList, answerArea)
        binding.fl.setAdapter(adapter)
        val subChildItemBeanXList = ques.ChildList!![0].ItemList!!
        val subItemAnswerList = ques.ModelAnswerInfoList!!
        if (ques.ChildList!![0].ItemList!!.size == 1) {
            binding.tvIndex.text = "${ques.ChildList!![0].ItemList!![0].ItemSortIndex}."
        } else if (answerArea.size > 1) {
            for (i in textInfoList.indices) {
                drawArea(i)
            }
        } else if (ques.ChildList!![0].ItemList!!.size == 1) {
            binding.tvIndex.text = "${ques.ChildList!![0].ItemList!![0].ItemSortIndex}."
        }
        try {
            for (i in subChildItemBeanXList.indices) {
                val itemAnswerList =
                    subItemAnswerList[i].Answer!!.split("$|")
                val stuAnswerInfo = subChildItemBeanXList[i].StuAnswer!!.split("@|")[0]
                if (stuAnswerInfo.isNotEmpty()) {
                    val stuPosition = stuAnswerInfo.split("*|")[0]
                    val stuType = stuAnswerInfo.split("*|")[1].toInt()
                    val stuAnswer = stuAnswerInfo.split("*|")[2]
                    //正确
                    if (itemAnswerList.contains(stuAnswerInfo)) {
                        if (stuPosition.contains("-")) {
                            val startPosition = stuPosition.split("-")[0].toInt()
                            val endPosition = stuPosition.split("-")[1].toInt()
                            val viewList = mutableListOf<View>()
                            textInfoList.forEachIndexed { index, textInfo ->
                                if (textInfo.Index in startPosition..endPosition) {
                                    viewList.add(binding.fl.getChildAt(index))
                                }
                            }
                            drawAnswer(
                                stuType,
                                0,
                                "",
                                "",
                                startPosition,
                                binding.fl.getChildAt(startPosition),
                                viewList
                            )
                        } else {
                            val position = getTextPosition(stuPosition.toInt())
                            drawAnswer(
                                stuType,
                                0,
                                if (stuType == 4) "无错" else stuAnswer,
                                "",
                                position,
                                binding.fl.getChildAt(position),
                                mutableListOf(binding.fl.getChildAt(position))
                            )
                        }
                    } else {
                        for (itemAnswerInfo in itemAnswerList) {
                            val itemPosition = itemAnswerInfo.split("*|")[0]
                            val itemType = itemAnswerInfo.split("*|")[1].toInt()
                            val itemAnswer = itemAnswerInfo.split("*|")[2]
                            if (itemPosition.contains("-")) {
                                val startPosition = itemPosition.split("-")[0].toInt()
                                val endPosition = itemPosition.split("-")[1].toInt()
                                val viewList = mutableListOf<View>()
                                textInfoList.forEachIndexed { index, textInfo ->
                                    if (textInfo.Index in startPosition..endPosition) {
                                        viewList.add(binding.fl.getChildAt(index))
                                    }
                                }
                                drawAnswer(
                                    itemType,
                                    0,
                                    "",
                                    "",
                                    startPosition,
                                    binding.fl.getChildAt(startPosition),
                                    viewList
                                )
                                drawAnswer(
                                    stuType,
                                    2,
                                    stuAnswer,
                                    "",
                                    getTextPosition(stuPosition.toInt()),
                                    binding.fl.getChildAt(getTextPosition(stuPosition.toInt())),
                                    mutableListOf(binding.fl.getChildAt(getTextPosition(stuPosition.toInt())))
                                )
                            } else {
                                if (stuPosition.contains("-")) {
                                    val startPosition = stuPosition.split("-")[0].toInt()
                                    val endPosition = stuPosition.split("-")[1].toInt()
                                    val viewList = mutableListOf<View>()
                                    textInfoList.forEachIndexed { index, textInfo ->
                                        if (textInfo.Index in startPosition..endPosition) {
                                            viewList.add(binding.fl.getChildAt(index))
                                        }
                                    }
                                    drawAnswer(
                                        stuType,
                                        2,
                                        "本区域无错",
                                        "",
                                        startPosition,
                                        binding.fl.getChildAt(startPosition),
                                        viewList
                                    )
                                    val position =
                                        getTextPosition(itemAnswerList[0].split("*|")[0].toInt())
                                    drawAnswer(
                                        itemAnswerList[0].split("*|")[1].toInt(),
                                        2,
                                        "未找出",
                                        itemAnswerList[0].split("*|")[2],
                                        position,
                                        binding.fl.getChildAt(position),
                                        mutableListOf(binding.fl.getChildAt(position))
                                    )
                                } else {
                                    if (stuPosition == itemPosition) {
                                        if (stuType == itemType) {
                                            drawAnswer(
                                                stuType,
                                                2,
                                                stuAnswer,
                                                itemAnswer,
                                                getTextPosition(stuPosition.toInt()),
                                                binding.fl.getChildAt(getTextPosition(stuPosition.toInt())),
                                                mutableListOf(
                                                    binding.fl.getChildAt(
                                                        getTextPosition(
                                                            stuPosition.toInt()
                                                        )
                                                    )
                                                )
                                            )
                                            break
                                        } else {
                                            drawAnswer(
                                                itemType,
                                                2,
                                                if (stuType == 4) "无错" else "未找出",
                                                if (itemType == 4) "无错" else itemAnswer,
                                                getTextPosition(itemPosition.toInt()),
                                                binding.fl.getChildAt(getTextPosition(itemPosition.toInt())),
                                                mutableListOf(
                                                    binding.fl.getChildAt(
                                                        getTextPosition(
                                                            itemPosition.toInt()
                                                        )
                                                    )
                                                )
                                            )
                                            drawAnswer(
                                                stuType,
                                                2,
                                                if (stuType == 4) "无错" else stuAnswer,
                                                "",
                                                getTextPosition(stuPosition.toInt()),
                                                binding.fl.getChildAt(getTextPosition(stuPosition.toInt())),
                                                mutableListOf(
                                                    binding.fl.getChildAt(
                                                        getTextPosition(
                                                            stuPosition.toInt()
                                                        )
                                                    )
                                                )
                                            )
                                            break
                                        }
                                    } else {
                                        drawAnswer(
                                            itemType,
                                            if (itemType == 0) 0 else 2,
                                            "未找出",
                                            if (itemType == 4) "无错" else itemAnswer,
                                            getTextPosition(itemPosition.toInt()),
                                            binding.fl.getChildAt(getTextPosition(itemPosition.toInt())),
                                            mutableListOf(
                                                binding.fl.getChildAt(
                                                    getTextPosition(
                                                        itemPosition.toInt()
                                                    )
                                                )
                                            )
                                        )
                                        drawAnswer(
                                            stuType,
                                            2,
                                            if (stuType == 4) "无错" else stuAnswer,
                                            "",
                                            getTextPosition(stuPosition.toInt()),
                                            binding.fl.getChildAt(getTextPosition(stuPosition.toInt())),
                                            mutableListOf(
                                                binding.fl.getChildAt(
                                                    getTextPosition(
                                                        stuPosition.toInt()
                                                    )
                                                )
                                            )
                                        )
                                        break
                                    }
                                }
                            }
                        }
                    }
                } else {
                    val itemPosition = itemAnswerList[0].split("*|")[0]
                    val itemType = itemAnswerList[0].split("*|")[1].toInt()
                    val itemAnswer = itemAnswerList[0].split("*|")[2]
                    if (itemPosition.contains("-")) {
                        val startPosition = itemPosition.split("-")[0].toInt()
                        val endPosition = itemPosition.split("-")[1].toInt()
                        val viewList = mutableListOf<View>()
                        textInfoList.forEachIndexed { index, textInfo ->
                            if (textInfo.Index in startPosition..endPosition) {
                                viewList.add(binding.fl.getChildAt(index))
                            }
                        }
                        drawAnswer(
                            itemType,
                            2,
                            if (ConstantBean.taskState == TaskState.LOOKING) "未找出" else "",
                            "本区域无错",
                            startPosition,
                            binding.fl.getChildAt(startPosition),
                            viewList
                        )
                    } else {
                        drawAnswer(
                            itemType,
                            if (itemType == 0) 0 else 2,
                            if (ConstantBean.taskState == TaskState.LOOKING) {
                                if (itemType == 4) "无错" else "未找出"
                            } else "",
                            if (itemType == 4) "无错" else itemAnswer,
                            getTextPosition(itemPosition.toInt()),
                            binding.fl.getChildAt(getTextPosition(itemPosition.toInt())),
                            mutableListOf(binding.fl.getChildAt(getTextPosition(itemPosition.toInt())))
                        )
                    }
                }
            }
        } catch (e: Exception) {

        } finally {

        }
    }

    private fun initAnswerData() {
        if (ConstantBean.constantExamBean == null) {
            return
        }
        val kindBeanList = ConstantBean.constantExamBean!!.TypeList
        typeIndex = requireArguments().getInt(AnswerConstant.KEY_TYPEINDEX)
        quesIndex = requireArguments().getInt(AnswerConstant.KEY_QUESINDEX)
        defaultLoadIndex = requireArguments().getInt("loadQuesIndex")
        ques = kindBeanList!![typeIndex].QuesList!![quesIndex]
        GenreType = ques.GenreType!!.toInt()
        GenreID = ques.GenreID!!.toInt()
        val answerAreaList =
            ques.ModelAnswerAreaList!!
        for (i in answerAreaList.indices) {
            val area = answerAreaList[i]
            answerArea.add(area)
        }
        textInfoList.addAll(ques.ModelTextInfoList!!)
    }

    /*绘制编号*/
    private fun drawArea(position: Int) {
        answerArea.forEachIndexed { index, it ->
            if (it.AnswerArea!!.split("-")[0].toInt() == textInfoList[position].Index) {
                val child = binding.fl.getChildAt(position)
                val tvIndex =
                    DrawView(
                        true,
                        false,
                        child,
                        ques.ChildList!![0].ItemList!![if (ques.ChildList!![0].ItemList!!.size > 1) index else 0].ItemSortIndex!!,
                        "",
                        -1,
                        1,
                        requireContext()
                    )
                binding.flLayout.addView(tvIndex)
            }
        }
    }

    /*绘制保存的作答*/
    @SuppressLint("UseCompatLoadingForDrawables")
    private fun drawAnswer(
        type: Int,
        color: Int,
        stuAnswer: String,
        answer: String,
        position: Int,
        child: View,
        list: MutableList<View>
    ) {
        val isFirst = isFirstPosition(position)
        when (type) {
            0, 1, 2, 3 -> {
                val answerView =
                    DrawView(true, isFirst, child, stuAnswer, answer, type, color, requireContext())
                binding.flLayout.addView(answerView)
            }
            4 -> {
                val answerView =
                    DrawView(true, isFirst, stuAnswer, answer, list, type, color, requireContext())
                binding.flLayout.addView(answerView)
            }
        }
    }

    /*是否是区域第一个*/
    private fun isFirstPosition(position: Int): Boolean {
        var isFirst = false
        if (answerArea.size == 10) {
            answerArea.forEach {
                if (textInfoList[position].Index == it.AnswerArea!!.split("-")[0].toInt()) {
                    isFirst = true
                }
            }
        }
        return isFirst
    }

    /**
     * 根据index找出position
     */
    private fun getTextPosition(index: Int): Int {
        var position = -1
        textInfoList.forEachIndexed { i, textInfo ->
            if (textInfo.Index == index) {
                position = i
            }
        }
        return position
    }

    fun adjustFontSize() {
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(binding.tvLeader,15)
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(binding.tvIndex,15)
    }
}